SETS-CREATION, OPERATIONS, AND SET METHODS
Sets in Python are unordered collections of unique elements. They are useful when you need to work with a collection of items while ensuring that each item appears only once. Here's how you can create sets, perform operations on them, and use some common set methods:
- Creating Sets: You can create a set by enclosing elements in curly braces
{}
or by using theset()
constructor. Note that duplicate elements are automatically removed.
# Using curly braces
my_set = {1, 2, 3}
# Using set() constructor
another_set = set([3, 4, 5])
-
Set Operations:
a. Union:
|
orunion()
Returns a new set containing all the elements that are in either of the sets.pythonset1 = {1, 2, 3} set2 = {3, 4, 5} # Using | union_set = set1 | set2 print(union_set) # Output: {1, 2, 3, 4, 5} # Using union() union_set = set1.union(set2) print(union_set) # Output: {1, 2, 3, 4, 5}
b. Intersection:
&
orintersection()
Returns a new set containing elements that are present in both sets.pythonset1 = {1, 2, 3} set2 = {3, 4, 5} # Using & intersection_set = set1 & set2 print(intersection_set) # Output: {3} # Using intersection() intersection_set = set1.intersection(set2) print(intersection_set) # Output: {3}
c. Difference:
-
ordifference()
Returns a new set containing elements that are present in the first set but not in the second set.pythonset1 = {1, 2, 3} set2 = {3, 4, 5} # Using - difference_set = set1 - set2 print(difference_set) # Output: {1, 2} # Using difference() difference_set = set1.difference(set2) print(difference_set) # Output: {1, 2}
d. Symmetric Difference:
^
orsymmetric_difference()
Returns a new set containing elements that are present in either of the sets but not in both.pythonset1 = {1, 2, 3} set2 = {3, 4, 5} # Using ^ symmetric_diff_set = set1 ^ set2 print(symmetric_diff_set) # Output: {1, 2, 4, 5} # Using symmetric_difference() symmetric_diff_set = set1.symmetric_difference(set2) print(symmetric_diff_set) # Output: {1, 2, 4, 5}
-
Set Methods:
a.
set.add(element)
: Adds an element to the set.pythonmy_set = {1, 2, 3} my_set.add(4) print(my_set) # Output: {1, 2, 3, 4}
b.
set.remove(element)
: Removes the specified element from the set. Raises a KeyError if the element is not found.pythonmy_set = {1, 2, 3} my_set.remove(2) print(my_set) # Output: {1, 3}
c.
set.discard(element)
: Removes the specified element from the set if it exists, but does nothing if the element is not found.pythonmy_set = {1, 2, 3} my_set.discard(2) my_set.discard(4) # No error, as 4 is not in the set print(my_set) # Output: {1, 3}
d.
set.pop()
: Removes and returns an arbitrary element from the set. Raises a KeyError if the set is empty.pythonmy_set = {1, 2, 3} removed_element = my_set.pop() print(removed_element) # Output: Randomly selected element (e.g., 1) print(my_set) # Output: The set with the randomly selected element removed
e.
set.clear()
: Removes all elements from the set, making it empty.pythonmy_set = {1, 2, 3} my_set.clear() print(my_set) # Output: set()
f.
set.copy()
: Returns a shallow copy of the set.pythonmy_set = {1, 2, 3} copied_set = my_set.copy() print(copied_set) # Output: {1, 2, 3}
These are some of the basic operations and methods you can use with sets in Python. Sets are particularly useful when you need to handle unique elements and perform set-based operations efficiently.